package gui.frame;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

public class FrameMoreListener extends Frame {
	Vector vector=new Vector();
	int i=0;
	Button btn1=new Button("Button 1");
	Button btn2=new Button("Button 2");
	
	FrameMoreListener(){
		this.setLayout(new FlowLayout());
		this.btn1.addActionListener(new B());
		this.btn1.addActionListener(new B1());
		this.btn2.addActionListener(new B());
		this.btn2.addActionListener(new B2());
		
		this.add(this.btn1);
		this.add(this.btn2);
	}
	
	class B implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("A button was pressed");
		}
		
	}
	
	class CountListener implements ActionListener{
		int index;
		public CountListener(int i){
			index=i;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("Counted Listener "+index);
		}
	}
	
	class B1 implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("Button 1 pressed");
			ActionListener listener=new CountListener(i++);
		}
		
	}
	
	class B2 implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("Button 2 pressed");
			int end=vector.size()-1;
			if(end>=0){
				btn2.removeActionListener((ActionListener) vector.elementAt(end));
				vector.removeElementAt(end);
			}
		}
		
	}
	public static void main(String[]args){
		Frame frame=new FrameMoreListener();
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		frame.setSize(300, 200);
		frame.setVisible(true);
	}

}
